package models_sports

import (
	"fmt"
	"math"
)

type (
	BasketBallMatchStatus uint32
	BasketballMatch       struct {
		ID             int                   `json:"id" gorm:"primaryKey;comment:比赛id"`
		CompetitionID  int                   `json:"competition_id"  gorm:"type:int(11);comment:赛事id"`
		HomeTeamID     int                   `json:"home_team_id"  gorm:"type:int(11);comment:主队id"`
		AwayTeamID     int                   `json:"away_team_id"  gorm:"type:int(11);comment:客队id"`
		Kind           int                   `json:"kind"  gorm:"type:int(11);comment:类型id，1-常规赛、2-季后赛、3-季前赛、4-全明星、5-杯赛、6-附加赛、0-无"`
		PeriodCount    int                   `json:"period_count"  gorm:"type:int(11);comment:比赛总节数(不包含加时)"`
		StatusID       BasketBallMatchStatus `json:"status_id"  gorm:"type:int(11);comment:比赛状态，详见状态码->比赛状态"`
		MatchTime      int64                 `json:"match_time"  gorm:"type:int(11);comment:比赛时间"`
		TimePlayed     uint32                `json:"time_played" gorm:"type:int(11); comment:进行时间"`
		Neutral        int                   `json:"neutral"  gorm:"type:int(11);comment:是否中立场，1-是、0-否s"`
		HomeScores     string                `json:"home_scores" gorm:"type:varchar(255);comment:主队比分"`
		AwayScores     string                `json:"away_scores" gorm:"type:varchar(255);comment:客队比分"`
		OverTimeScores string                `json:"over_time_scores" gorm:"type:varchar(255);comment:加时赛比分"`
		HomePosition   string                `json:"home_position" gorm:"type:varchar(255);comment:主队排名"`
		AwayPosition   string                `json:"away_position" gorm:"type:varchar(255);comment:客队排名"`
		Mlive          int                   `json:"mlive" gorm:"type:int(11);comment:是否有动画，1-是、0-否"`
		Intelligence   int                   `json:"intelligence" gorm:"type:int(11);comment:是否有情报，1-是、0-否"`
		VenueID        int                   `json:"venue_id" gorm:"type:int(11);comment:场馆id"`
		SeasonID       int                   `json:"season_id" gorm:"type:int(11);comment:赛季id"`
		StageID        int                   `json:"stage_id" gorm:"type:int(11);comment:阶段id"`
		GroupNum       int                   `json:"group_num" gorm:"type:int(11);comment:第几组，1-A、2-B以此类推"`
		RoundNum       int                   `json:"round_num" gorm:"type:int(11);comment:第几轮"`
		UpdatedAt      int                   `json:"updated_at" gorm:"type:int(11);comment:更新时间"`
	}
)

// GetTimePlayed 比赛进行分钟数获取公式
// 每节是12分钟
func (f BasketBallMatchStatus) GetTimePlayed(kTime int) uint32 {
	if kTime == 0 {
		return 0
	}
	result := kTime / 60
	ret := uint32(math.Ceil(float64(result)))
	return ret
}

// ToString 状态转换
func (f BasketBallMatchStatus) ToString() string {
	statusMap := map[uint32]string{
		0:  "比赛异常",
		1:  "未开赛",
		2:  "第一节",
		3:  "第一节完",
		4:  "第二节",
		5:  "第二节完",
		6:  "第三节",
		7:  "第三节完",
		8:  "第四节",
		9:  "加时",
		10: "完场",
		11: "中断",
		12: "取消",
		13: "延期",
		14: "腰斩",
		15: "待定",
	}
	s, ok := statusMap[uint32(f)]
	if !ok {
		return fmt.Sprintf("未知:%d", uint32(f))
	}
	return s
}

// MatchTempScheduleStatus 状态转换
func (f BasketBallMatchStatus) MatchTempScheduleStatus() uint32 {
	// 比赛状态, 1-进行,2-未开赛,3-完场
	if f <= 1 {
		return 2 // 2-未开赛
	}
	if f == 10 {
		return 3 // 3-完场
	}
	if f > 1 && f < 10 {
		return 1 // 1-进行
	}
	if f > 10 && f <= 15 {
		return 4 // 4-异常
	}
	return 0
}

func (m BasketballMatch) GetStartingStatus() []int {
	return []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
}
